﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Timers;
using DAQ.Config;
using DAQ.ModbusModule;
using DAQ.MqttModule;
using Microsoft.VisualBasic.FileIO;
using Timer = System.Timers.Timer;

namespace DAQ
{
    public class DaqService
    {
        private DaqOption _option;
        private List<DeviceLink> _deviceLinks;
        private ServiceConfig _serviceConfig;

        MqttControllor mqttControllor { get; set; }

        private Timer _timer;

        public DaqService(DaqOption option)
        {
            _option = option;
            _deviceLinks = option.DeviceLinks;
            _serviceConfig = option.ServiceConfig;
            if (_serviceConfig.IsPushScheduled)
            {
                _timer = new Timer(_serviceConfig.PushTimeSpan * 1000);
                _timer.Elapsed += Timer_Elapsed;
            }
        }

        public void Start()
        {
            foreach (var item in _deviceLinks)
            {
                ModbusTcp modbusTcp = new ModbusTcp(item);
                modbusTcp.ValueUpdated += ModbusTcp_ValueUpdated;
                modbusTcp.DoMitor();
            }

            mqttControllor = new MqttControllor(_option.MqttConfig);
            mqttControllor.MqttConnect();
            if (_serviceConfig.IsPushScheduled)
            {
                _timer.Start();
            }
        }

        private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
        {
            foreach (var link in _deviceLinks)
            {
                try
                {
                    DeviceMessage deviceMessage = new DeviceMessage() { DeviceId = link.UID };
                    foreach (var point in link.Points)
                    {
                        Console.WriteLine($"Point:{point.UID}-->Value:{point.Value}");
                        deviceMessage.Data.Add(point.UID, point.Value);
                    }
                    var str1 = JsonSerializer.Serialize(deviceMessage);
                    mqttControllor.Publish("Device/" + link.UID + "/Time", str1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        //事件方法
        private void ModbusTcp_ValueUpdated(RegisterPoint point, object value)
        {
            if (_serviceConfig.IsPushChanged)
            {
                try
                {
                    DeviceMessage device = new DeviceMessage { DeviceId = point.DeviceId };
                    device.Data.Add(point.UID, value);
                    var data = JsonSerializer.Serialize(device);
                    mqttControllor.Publish($"Device/{point.DeviceId}/Update", data); //采集立刻推送
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine($"Point:{point.UID}-->Value:{value}");
            }
        }
    }
}
